<?php
// 查找带回
class BringBackAction extends CommonAction {
	//私人参数
	private static $catNodeList       = array();
	private static $catTreeFolderHTML = '';
	//私人函数
	private function treeBuild($data, $active = true) {
		$catNodeList                  = array();
		$rootnode                     = new stdClass();
		$rootnode->parent             = null;
		$rootnode->children           = array();
		$rootnode->self->categoryId   = 0;
		$rootnode->self->categoryName = '根节点';
		$rootnode->self->level        = 0;
		$catNodeList[]                = $rootnode;
		foreach ($data as $key => $value) {
			if ($active && $value['status'] == 10) {
				continue;
			}
			if (!isset($catNodeList[$value['category_id']])) {
				$catNodeList[$value['category_id']] = new stdClass();
			}
			if (!isset($catNodeList[$value['up_category_id']])) {
				self::$catNodeList[$value['up_category_id']] = new stdClass();
			}
			if (!isset($catNodeList[$value['up_category_id']]->children)) {
				$catNodeList[$value['up_category_id']]->children = array();
			}
			$catNodeList[$value['category_id']]->self->categoryId   = $value['category_id'];
			$catNodeList[$value['category_id']]->self->categoryName = $value['category_name'];
			$catNodeList[$value['category_id']]->self->level        = $value['level'];
			$catNodeList[$value['category_id']]->self->categoryType = $value['category_type'];
			$catNodeList[$value['category_id']]->parent             = $value['up_category_id'];
			$catNodeList[$value['up_category_id']]->children[]      = $value['category_id'];
		}
		return $catNodeList;
	}
	private function createcatTreeFolderHTML($node, $category_type) {
		if ($node->self->categoryType != $category_type && $node->self->categoryId != 0) {
			return false;
		}
		if (!isset($node->self->categoryId) || !isset($node->self->categoryName)) {
			return false;
		}
		$this->catTreeFolderHTML .= '<li class="folder"><a onclick="$.bringBack({categoryId:\'' . $node->self->categoryId . '\', categoryName:\'' . $node->self->categoryName . '\'})">' . $node->self->categoryName . '</a>';
		if (empty($node->children)) {
			return false;
		} else {
			$this->catTreeFolderHTML .= '<ul>';
			foreach ($node->children as $k => $id) {
				$this->createcatTreeFolderHTML(self::$catNodeList[$id], $category_type);
			}
			$this->catTreeFolderHTML .= '</ul>';
		}
		$this->catTreeFolderHTML .= '</li>';
		return true;
	}
	//select入口
	public function SelectCategory() {
		$map['category_type'] = $_REQUEST['category_type'];
		$model                = M('CategoryInfo');
		self::$catNodeList    = $this->treeBuild($model->where($map)->select());

		self::$catNodeList[0]->self->categoryName = C('APP_CATEGORY.'.$map['category_type']);
		$this->createcatTreeFolderHTML(self::$catNodeList[0], $map['category_type']);
		$this->assign('str', $this->catTreeFolderHTML);
		$this->display();
	}
}
?>